package hotel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

import java.io.IOException;

public class createAccount implements ActionListener
{
	protected static JFrame testframe; // static so the class progressBar can dispose of it. 
	private JPanel panel;
	private JFrame loginFrame;
	private JTextField idField, codeField;
	private JPasswordField passwordField;
	private JButton loginButton, cancelButton;
	private String type;


	/**
	 * This constructor creates a login window, and also adds components to the content pane.
	 */
	public createAccount(String type1){
		type = type1;
		panel = new JPanel(new BorderLayout());
		try {
			UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}

		loginFrame = new JFrame("Create Account");
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		loginFrame.setSize(new Dimension(700, 150));
		loginFrame.setResizable(false);
		loginFrame.setLocation((int) (screenSize.width*0.25), (int) (screenSize.height*0.25));
		loginFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		loginFrame.setLayout(new BorderLayout());

		ImageIcon icon = new ImageIcon("Welcome.png"); 
		JLabel label = new JLabel(); 

		label.setIcon(icon); 
		label.setHorizontalAlignment(label.CENTER);

		JPanel test = new JPanel();
		test.setBackground(Color.white);
		test.setForeground(Color.white);
		test.add(label, BorderLayout.NORTH); 
		createIdAndPasswordField();
		createButtons();
		loginFrame.add(panel, BorderLayout.CENTER);
		loginFrame.setVisible(true);
	}
	private void createIdAndPasswordField()
	{
		JPanel passwordPanel = new JPanel(new FlowLayout());
		passwordPanel.setBackground(Color.white);
		JPanel idPanel = new JPanel();
		JPanel codePanel = new JPanel();
		idField = new JTextField(20);
		idField.setEditable(true);
		idField.setCaretPosition(0);

		idField.setBorder(new LineBorder(Color.BLACK, 1, true));

		codeField = new JTextField(30);
		codeField.setEditable(true);
		codeField.setCaretPosition(0);

		codeField.setBorder(new LineBorder(Color.BLACK, 1, true));
		JTextArea idTxtArea = new JTextArea("Username :");
		idTxtArea.setEditable(false);
		idTxtArea.setHighlighter(null);
		idPanel.add(idTxtArea);
		idPanel.add(idField);

		idPanel.setBackground(Color.white);


		passwordField = new JPasswordField(20);
		passwordField.setEditable(true);
		passwordField.setCaretPosition(0);
		passwordField.setBorder(new LineBorder(Color.BLACK, 1, true));


		JTextArea passwordTxtArea = new JTextArea("Password :");
		passwordTxtArea.setEditable(false);
		passwordTxtArea.setHighlighter(null);


		JTextArea codeTxtArea = new JTextArea("License Code : ");
		codeTxtArea.setEditable(false);
		codeTxtArea.setHighlighter(null);


		codePanel.add(codeTxtArea);
		codePanel.add(codeField);

		passwordPanel.add(passwordTxtArea);
		passwordPanel.add(passwordField);

		panel.add(idPanel, BorderLayout.NORTH);
		panel.add(passwordPanel, BorderLayout.CENTER);
		panel.setBackground(Color.white);
	}

	/**
	 * Void methods that creates the buttons and adds them to the panel.
	 */
	public void createButtons()
	{
		JPanel buttonPanel = new JPanel(new FlowLayout());
		loginButton = new JButton("Login");
		loginButton.setActionCommand("Login");
		loginButton.addActionListener(this);
		cancelButton = new JButton("Clear");
		cancelButton.setActionCommand("Clear");
		cancelButton.addActionListener(this);
		JButton CreateButton = new JButton();
		CreateButton = new JButton("Create Account");
		CreateButton.setActionCommand("Create");
		CreateButton.addActionListener(this);
		//buttonPanel.add(loginButton);
		buttonPanel.add(new JSeparator());
		//buttonPanel.add(cancelButton);
		buttonPanel.add(CreateButton);

		panel.add(buttonPanel, BorderLayout.SOUTH);
		Container cp = loginFrame.getContentPane();
		cp.setBackground(Color.white);
	}

	private boolean createUser(String name, String pass) throws IOException
	{
		User u = new User(name, pass);
		return loginWindow.database.addUser(u);
	}

	private boolean createOwner(String name, String pass) throws IOException{
		Owner o = new Owner(name, pass);
		return loginWindow.database.addOwner(o);
	}

	/**
	 * Action Performed method that overrides actionListener's actionPerformed method.  
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		String c = e.getActionCommand();

		if(c.equals("Create")) //attempt to login
		{
			{
				try {
					if(type.equals("User"))
					{
						if(!createUser(idField.getText(), passwordField.getText()))
							JOptionPane.showMessageDialog(null, "User name already exists!", "Account Not Created",  JOptionPane.INFORMATION_MESSAGE);
						else
						{
							JOptionPane.showMessageDialog(null, "Account created successfully!", "Account Created",  JOptionPane.INFORMATION_MESSAGE);
							loginFrame.dispose();
						}
					}
					else if(type.equals("Hotel Owner")){
						if(createOwner(idField.getText(), passwordField.getText()))
							JOptionPane.showMessageDialog(null, "Owner name already exists!", "Account Not Created",  JOptionPane.INFORMATION_MESSAGE);
						else 
						{
							JOptionPane.showMessageDialog(null, "Account created successfully!", "Account Created",  JOptionPane.INFORMATION_MESSAGE);
							loginFrame.dispose();
						}
					}
				} catch (IOException e1) {
				}


			}
		}
	}
}
